/**
 * 
 */
define(['wiseMen/module/AdminDivTree'],function(AdminDivTree){
	webUtils.nameSpace("wiseMen.platform.adminDivSelector");
	wiseMen.platform.adminDivSelector.main=function(params){
		var adminDivTree;
		var hasSelectedGrid;
		params = $.extend({
	        scope:this,
	        hasSelectedAdminDivs:[],
	        initAdminDivs:null,
	        isSingle:false,
	        onSelect:function(rows){}
	    }, params);
		
		var dialog =webUtils.dialog({
            title: '选择行政区划',
            onremove: function () {
                if (params.onClose) {
                    params.onClose.call(null);
                }
            },
            buttons: [{
            	label: '关闭',
                action: function (dialog) {
                    dialog.close();
                }
            },{
            	label: '确定',
            	icon: 'glyphicon glyphicon-check',
                cssClass: 'btn-primary',
                action: function (dialog) {
                    var rows=hasSelectedGrid.bootstrapTable("getData");
                    params.onSelect.call(params.scope,rows);
                    dialog.close();
                }
            }],
            onshown:function(dialog){
            	var body = dialog.getModalBody();
        	    body.append(wiseMen.platform.adminDivSelector.temp);
        	    var mainContent = body.find(".mainContent");

        	    adminDivTree = new AdminDivTree({
        	        target: mainContent.find("._adminDivTree"),
        	        initAdminDivs:params.initAdminDivs,
        	        onClick: function (e, treeId, treeNode, clickFlag) {
        	            if (clickFlag != 0) {
        	                onSelectAdminDiv(treeNode);
        	            }
        	        }
        	    });
        	    
        	    hasSelectedGrid=new wiseMen.platform.adminDivSelector.hasSelectedAdminDivBox({
        	        target: mainContent.find("._hasSelectedBox"),
        	        hasSelectedAdminDivs:params.hasSelectedAdminDivs
        	    });
            }
        });
		
		function onSelectAdminDiv(treeNode) {
	        if(!treeNode.code){return;}
	        var row= hasSelectedGrid.bootstrapTable(
					"getRowByUniqueId", treeNode.code);
	        
	        if(!row){
	        	if(params.isSingle){
	            	hasSelectedGrid.bootstrapTable('removeAll');
	            }
	        	
	            hasSelectedGrid.bootstrapTable("append", treeNode);
	        }
	    }
	};
	
	wiseMen.platform.adminDivSelector.hasSelectedAdminDivBox = function (params) {
	    params = $.extend({
	        hasSelectedAdminDivs:[]
	    }, params);
	    var grid = params.target.bootstrapTable({
	        columns: [
	                {title: '行政区划', field: 'name', align: 'left',width:300},
	                {title: '编码', field: 'code', align: 'left',width:300}
	            ],
	        data:params.hasSelectedAdminDivs,
	        sidePagination : 'client',
			pagination : false,
			uniqueId:"code",
			onClickRow : function(row) {
				grid.bootstrapTable("removeByUniqueId", row.code);
			}
	    });
	    
	    return grid;
	};

	wiseMen.platform.adminDivSelector.temp=
        '<div class="mainContent row">'
        + '<div title="行政区划" class="col-md-6"><div class="_adminDivTree ztree"></div></div>'
        + '<div title="已选列表" class="col-md-6" ><table class="_hasSelectedBox"></table></div>'
        + '</div>';
	return wiseMen.platform.adminDivSelector.main;
});